package work.xkong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Course;
import work.xkong.entity.Student;
import work.xkong.entity.Teacher;
import work.xkong.mapper.CourseMapper;
import work.xkong.mapper.StudentCourseMapper;
import work.xkong.mapper.StudentMapper;
import work.xkong.mapper.TeacherMapper;
import work.xkong.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import work.xkong.vo.inputvo.CourseSelectInputVo;
import work.xkong.vo.outputvo.*;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author NuanYang
 * @since 2022-11-21
 */
@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public ResponseRest getSelectCourse(CourseSelectInputVo inputVo) {
        PageHelper.startPage(inputVo.getCurrentPage(),inputVo.getPageSize());

        List<CourseSelectOutputVo> outputVo = courseMapper.getCourseSelect(inputVo);

        PageInfo<CourseSelectOutputVo> info = new PageInfo<>(outputVo);

        return ResponseRest.creatSuccess(info);
    }

    @Override
    public ResponseRest getStudentCourse(CourseSelectInputVo inputVo) {
        PageHelper.startPage(inputVo.getCurrentPage(),inputVo.getPageSize());

        List<StudentCourseOutputVo> outputVo = courseMapper.getStudentSelect(inputVo);

        PageInfo<StudentCourseOutputVo> info = new PageInfo<>(outputVo);

        return ResponseRest.creatSuccess(info);
    }

    @Override
    public ResponseRest getStudentScoreCourse(CourseSelectInputVo inputVo) {
        PageHelper.startPage(inputVo.getCurrentPage(),inputVo.getPageSize());

        List<StudentCourseOutputVo> outputVo = courseMapper.getStudentScore(inputVo);

        PageInfo<StudentCourseOutputVo> info = new PageInfo<>(outputVo);

        return ResponseRest.creatSuccess(info);
    }

    @Override
    public ResponseRest getCourseTime(String studentId) {

        //判断学生是否存在
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStudentId, studentId));
        if (student == null) {
            return ResponseRest.creatFail("该学员不存在，非法请求");
        }

        List<CourseTableTime> courseTableTime = courseMapper.getCourseTableTime(studentId);
        System.out.println(courseTableTime);

        return ResponseRest.creatSuccess(courseTableTime);
    }

    @Override
    public ResponseRest getTeacherCourses(CourseSelectInputVo inputVo) {
        PageHelper.startPage(inputVo.getCurrentPage(),inputVo.getPageSize());

        List<TeacherCourseOutputVo> outputVo = courseMapper.getTeacherSelect(inputVo);

        PageInfo<TeacherCourseOutputVo> info = new PageInfo<>(outputVo);

        return ResponseRest.creatSuccess(info);
    }

    @Override
    public ResponseRest getTeacherCourseTime(String teacherId) {
        //判断教师是否存在
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherId, teacherId));
        if (teacher == null) {
            return ResponseRest.creatFail("该教师不存在，非法请求");
        }

        List<CourseTableTime> courseTableTime = courseMapper.getCourseTeacherTableTime(teacherId);
        System.out.println(courseTableTime);

        return ResponseRest.creatSuccess(courseTableTime);
    }

    @Override
    public ResponseRest getTeacherClassCourses(CourseSelectInputVo inputVo) {
        PageHelper.startPage(inputVo.getCurrentPage(),inputVo.getPageSize());

        List<TeacherCourseSaveOutputVo> outputVo = courseMapper.getTeacherScore(inputVo);

        PageInfo<TeacherCourseSaveOutputVo> info = new PageInfo<>(outputVo);

        return ResponseRest.creatSuccess(info);
    }


}
